package com.company.gas.service.impl;


import com.company.gas.mapper.GasBaseMapper;
import com.company.gas.mapper.GasDeliMapper;
import com.company.gas.pojo.GasBase;
import com.company.gas.pojo.GasDeli;
import com.company.gas.service.IGasDeliService;
import com.company.gas.util.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class GasDeliServiceImpl implements IGasDeliService {
    @Autowired
    private GasDeliMapper gasDeliMapper;
    @Autowired
    private GasBaseMapper gasBaseMapper;
    //查询全部调度
    @Override
    public List<GasDeli> queryGasDeliList(){
        return gasDeliMapper.queryGasDeliList();
    }
    //查询单个调度
    @Override
    public GasDeli queryGasDeliByID(String deliId){
        GasDeli gasDeli = gasDeliMapper.queryGasDeliByID(deliId);
        return gasDeli;
    }
    //添加调度，级联修改燃气库表信息
    @Override
    public int addGasDeli(String deliId, String deliStart, String deliFinish, BigDecimal deliAmount, String createBy) {
        GasBase deliBaseS = gasBaseMapper.queryGasBaseByID(deliStart);
        GasBase deliBaseF = gasBaseMapper.queryGasBaseByID(deliFinish);
        BigDecimal gasStoreS = deliBaseS.getGasStore();
        BigDecimal gasStoreF = deliBaseF.getGasStore();
        String baseAdrS = deliBaseS.getBaseAdr();
        String baseAdrF = deliBaseF.getBaseAdr();
        gasBaseMapper.updateGasBase(deliStart, BigDecimalUtil.subtract(gasStoreS,deliAmount),baseAdrS,createBy);
        gasBaseMapper.updateGasBase(deliFinish,BigDecimalUtil.accumulate(gasStoreF,deliAmount),baseAdrF,createBy);
        gasDeliMapper.addGasDeli(deliId, deliStart, deliFinish,deliAmount, createBy);
        return 1;
    }
}
